using Godot;
using Godot.Collections;
using System;

namespace EnemyStateNode
{
public partial class ThrowBarb : Node,StateNode
{
	public Enemy enemy;

	public AnimatedSprite2D animaEnemy;

	public Barb barb;

	public override void _Ready()
	{
		enemy = GetNode<Enemy>("/root/Main/Enemy");

		animaEnemy = enemy.GetNode<AnimatedSprite2D>("AnimaEnemy");
		animaEnemy.AnimationFinished+=OnFinished;

		barb = enemy.GetNode<Barb>("/root/Main/Enemy/Weapons/Barb");
	}

	public void OnThrowBarb()
	{
		int randomNum =  (int)MyUtil.RangeRandom(3,6);
		Barb temp;
		Vector2 window = enemy.GetViewportRect().Size;
		for(int i =1 ; i<=randomNum; i++)
		{
			temp = barb.Duplicate() as Barb;
			enemy.AddChild(temp);
			
			temp.Animation = "default";
			temp.Visible = true;
			temp.TopLevel = true;
			float randX = MyUtil.RangeRandom(window.X/randomNum*(i-1),window.X);
			float randY = MyUtil.RangeRandom(250,500);
			
			temp.timer.WaitTime = MyUtil.RangeRandom(3,5);
			temp.timer.Start();

			temp.area2D.SetCollisionMaskValue(2,true);

			temp.Position = new(randX,randY);
			temp.Play();
		}
	}

	private void OnFinished()
	{
		if(animaEnemy.Animation == "throw_barb")
			enemy.SwitchTo("Idle");
	}


	public void OnEnter()
	{
		animaEnemy.Animation="throw_barb";

		GetNode<AudioStreamPlayer2D>("Music").Play();

		OnThrowBarb();
	}
    public void OnExit(){}

	public void OnUpdate(float delta)
	{
	}
}
}
